<template>
  <div class="merchant" ref="marchant">
    <div class="merchant-content">
      <div class="over">
        <div class="con-left">
          <h1 class="total">{{seller.score}}</h1>
          <h2 class="zh-total">综合评分</h2>
          <p class="top-total">高于周边商家{{seller.rankRate}}%</p>
        </div>
        <!-- 分数星星 -->
        <div class="con-right">
          <div class="score-wrapper">
            <span class="title">服务态度</span>
            <star :size="48" :score="seller.score"></star>
            <span class="score">{{seller.score}}</span>
          </div>
          <div class="score-wrapper">
            <span class="title">商品评分</span>
            <star :size="48" :score="seller.foodScore"></star>
            <span class="score">{{seller.foodScore}}</span>
          </div>
          <div class="score-wrapper">
            <span class="title">送达时间</span>
            <span class="deliveyTime">{{seller.deliveryTime}}分钟</span>
          </div>
        </div>
      </div>
      <!-- <split></split> -->
      <rating
        :select-type="selectType"
        :only-content="onlyContent"
        :desc="desc"
        :ratings="ratings"
        @sen="sendSelect"
        @li="sendonlyindex"
      ></rating>
      <!-- 评论内容 -->
      <div class="mer-con">
        <ul>
          <li
            v-for="(rating,index) in ratings"
            :key="index"
            class="rating-li"
            v-show="needShow(rating.rateType,rating.text)"
          >
            <div class="avatar-img">
              <img :src="rating.avatar" alt width="28px" height="28px">
            </div>
            <div class="rating-right-con">
              <p class="name-time">
                <span class="username">{{rating.username}}</span>
                <span class="time">{{rating.rateTime | formateDates}}</span>
              </p>
              <p class="bian-xx">
                <star :size="48" :score="rating.score"></star>
                <span v-if="rating.deliveryTime" class="deliveryTime">{{rating.deliveryTime}}分钟送达</span>
              </p>

              <p class="text">{{rating.text}}</p>
              <p class="icon-label">
                <span
                  class="iconfont"
                  :class="{'icon-haoping':rating.rateType===0,'icon-ai46':rating.rateType===1}"
                ></span>
                <span
                  class="label"
                  v-for="(iconcon,index) in rating.recommend"
                  :key="index"
                >{{iconcon.length >= 4? iconcon.substr(0,4)+"...":iconcon}}</span>
                <!-- 判断文字超过没超过4个,如果超过就截取在后面加...然后返回 -->
              </p>
            </div>
          </li>
        </ul>
        <div class="no-rating" v-show="!ratings || !ratings.length">暂无评价</div>
      </div>
    </div>
  </div>
</template>

<script>
import star from "../star/star";
import split from "../split/split";
import rating from "../rating/rating";
import BScroll from "better-scroll";
import { formatDate } from "../../../public/js/date.js";

const POSITIVE = 0;
const NEGATIVE = 1;
const ALL = 2;
export default {
  components: {
    star,
    rating,
    split
  },
  props: {
    seller: {
      type: Object
    }
  },
  data() {
    return {
      ratings: [],
      selectType: ALL,
      onlyContent: true,
      desc: {
        all: "全部",
        positive: "满意",
        negative: "不满意"
      },
      response: {}
    };
  },
  //时间戳
  filters: {
    formateDates(time) {
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm");
    }
  },
  created() {
    this.axios.get("http://localhost:3000/api/ratings").then(res => {
      if (res.status === 200) {
        //判断接到数据
        this.ratings = res.data.ratings;
        //初始化BScroll
        this.$nextTick(() => {
          if (!this.scroll) {
            this.scroll = new BScroll(this.$refs.marchant, {
              click: true
            });
          }
        });
      } else {
        alert("数据没有请求到");
      }
    });
  },
  methods: {
    //tab切换
    sendSelect(data) {
      if (data == 0) {
        this.selectType = POSITIVE;
      } else if (data == 1) {
        this.selectType = NEGATIVE;
      } else if (data == 2) {
        this.selectType = ALL;
      }
      this.selectType = data;
      this.$nextTick(() => {
        this.scroll.refresh();
      });
    },

    //对号选择
    sendonlyindex(data) {
      this.onlyContent = data;
      this.$nextTick(() => {
        this.scroll.refresh();
      });
    },

    //是否显示评论,为空不显示
    needShow(type, text) {
      if (this.onlyContent && !text) {
        return false;
      }
      if (this.selectType === ALL) {
        return true;
      } else {
        return type === this.selectType;
      }
    }
  }
};
</script>

<style lang="scss">
.merchant {
  position: absolute;
  top: 176px;
  left: 0;
  bottom: 0;
  width: 100%;
  overflow: hidden;
  background: #f3f5f7;
  .merchant-content {
    .over {
      display: flex;
      padding: 18px 0;
      background: #fff;
      margin-bottom: 18px;
      border-bottom: 1px solid rgba(7, 17, 27, 0.1);
      .con-left {
        flex: 0 0 137px;
        border-right: 1px solid rgba(7, 17, 27, 0.4);
        text-align: center;
        @media only screen and (max-width: 320px) {
          flex: 0 0 120px;
          width: 120px;
        }
        .total {
          font-size: 24px;
          color: rgb(255, 153, 0);
          line-height: 28px;
          margin-bottom: 6px;
        }
        .zh-total {
          font-size: 12px;
          color: rgb(7, 17, 27);
          line-height: 12px;
          margin-bottom: 8px;
        }
        .top-total {
          font-size: 10px;
          color: rgb(147, 153, 159);
          line-height: 20px;
          margin-bottom: 8px;
        }
      }
      .con-right {
        flex: 1;
        padding-left: 24px;
        @media only screen and (max-width: 320px) {
          padding-left: 6px;
        }
        .score-wrapper {
          margin-bottom: 8px;
          font-size: 0;
          .title {
            display: inline-block;
            vertical-align: top;
            font-size: 12px;
            color: rgb(7, 17, 27);
            line-height: 18px;
          }
          .star,
          .deliveyTime {
            display: inline-block;
            vertical-align: top;
            margin-left: 12px;
            @media only screen and (max-width: 320px) {
              margin-left: 6px;
            }
          }
          .star {
            margin-right: 6px;
            .star-item {
              margin-right: 5px;
              @media only screen and (max-width: 320px) {
                margin-right: 0;
              }
            }
          }
          .score {
            font-size: 13px;
            color: rgb(255, 153, 0);
            line-height: 20px;
          }
          .deliveyTime {
            font-size: 12px;
            color: rgb(147, 153, 159);
          }
        }
      }
    }
    .mer-con {
      background: #fff;
      padding: 0 18px;
      .rating-li {
        width: 100%;
        display: flex;
        padding: 18px 0;
        border-bottom: 1px solid rgba(7, 17, 27, 0.1);
        .avatar-img {
          padding-right: 12px;
          img {
            border-radius: 50%;
          }
        }
        .rating-right-con {
          width: 100%;
          .name-time {
            display: flex;
            justify-content: space-between;
            .username {
              font-size: 10px;
              color: rgb(7, 17, 27);
              line-height: 12px;
            }
            .time {
              font-size: 10px;
              font-weight: 200;
              color: rgb(147, 153, 159);
              line-height: 12px;
            }
          }
          .bian-xx {
            margin-top: 4px;
            display: flex;
            justify-content: start;
            align-items: center;
            .star-item {
              margin-right: 6px;
            }
            .deliveryTime {
              font-size: 10px;
              font-weight: 200;
              color: rgb(147, 153, 159);
            }
          }
          .text {
            margin-top: 6px;
            font-size: 12px;
            color: rgb(7, 17, 27);
            line-height: 18px;
          }
          .icon-label {
            margin-top: 8px;
            .icon-haoping {
              color: rgb(0, 160, 220);
              margin-right: 4px;
              line-height: 24px;
              font-size: 15px;
            }
            .icon-ai46 {
              color: rgb(147, 153, 159);
              margin-right: 4px;
              line-height: 24px;
              font-size: 15px;
            }
            .label {
              display: inline-block;
              font-size: 9px;
              color: rgb(147, 153, 159);
              line-height: 16px;
              padding: 0 6px;
              margin-left: 8px;
              border: 1px solid rgba(7, 17, 27, 0.1);
              border-radius: 2px;
              background: rgb(255, 255, 255);
            }
          }
        }
      }
      .no-rating {
        padding: 16px 0;
        font-size: 12px;
        color: rgb(147, 153, 159);
      }
    }
  }
}
</style>